package com.aaa.outbound.controller;


import com.aaa.entity.outbound.*;
import com.aaa.entity.stock.Stock;
import com.aaa.outbound.feign.StockFeign;
import com.aaa.outbound.service.*;
import com.aaa.vo.R;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author aaa
 * @since 2020-12-03
 */
@RestController
@RequestMapping("/outbound/review-detail")
public class ReviewDetailController {
    @Autowired
    private ReviewDetailService reviewDetailService;
    @Autowired
    private PickDetailService pickDetailService;
    @Autowired
    private ReviewService reviewService;
    @Autowired
    private OutorderService outorderService;
    @Autowired
    private StockFeign stockFeign;

    @PostMapping("addReviewDetail/{outorderno}/{commoditybar}/{qty}")
    @ApiOperation(value = "录入出库复核明细")
    public R addReviewDetail(@ApiParam(value = "商品编码")@PathVariable("commoditybar") String commoditybar,
                             @ApiParam(value = "数量")@PathVariable("qty") Integer qty,
                             @ApiParam(value = "出库订单编号")@PathVariable("outorderno")String outorderno){
        QueryWrapper<Review> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("outorderno",outorderno);
        Review review = reviewService.getOne(queryWrapper);
        Long pid = review.getId();
        QueryWrapper<ReviewDetail> queryWrapper1=new QueryWrapper<>();
        queryWrapper1.eq("pid",pid);
        queryWrapper1.eq("commoditybar",commoditybar);
        queryWrapper1.eq("qty",qty);
        ReviewDetail one = reviewDetailService.getOne(queryWrapper1);
        if(one!=null){
            return R.error().message("请勿重复录入");
        }
        QueryWrapper<Outorder> queryWrapper2=new QueryWrapper<>();
        queryWrapper2.eq("outorderno",outorderno);
        List<PickDetail> pickDetails = pickDetailService.selectPickDetailByOutorderno(outorderno);
        for(PickDetail pickDetail:pickDetails){
            if (pickDetail.getCommoditybar().equals(commoditybar)&&pickDetail.getQty()==qty) {
                ReviewDetail reviewDetail=new ReviewDetail();
                reviewDetail.setPid(review.getId());
                reviewDetail.setCommodityid(pickDetail.getCommodityid());
                reviewDetail.setCommoditybar(pickDetail.getCommoditybar());
                reviewDetail.setCommodityname(pickDetail.getCommodityname());
                reviewDetail.setQty(qty);
                Date now=new Date();
                reviewDetail.setCreatetime(now);
                reviewDetailService.save(reviewDetail);
                review.setState("2");
                reviewService.updateById(review);
                return R.ok().code(200);
            }
        }
        return R.error().message("商品信息不匹配");
    }

    @PostMapping("viewReviewDetail/{outorderno}")
    @ApiOperation(value = "查看复核明细")
    public R viewReviewDetail(@ApiParam(value = "出库订单编号")@PathVariable("outorderno") String outorderno){
        List<ReviewDetail> reviewDetails = reviewDetailService.selectReviewDetailByOutorderno(outorderno);
        return R.ok().data("reviewDetails",reviewDetails).code(200);
    }

    @PostMapping("endReviewDetail/{outorderno}")
    @ApiOperation(value = "结束录入明细")
    public R endReviewDetail(@ApiParam(value = "出库订单编号")@PathVariable("outorderno") String outorderno){
        List<PickDetail> pickDetails = pickDetailService.selectPickDetailByOutorderno(outorderno);
        for(PickDetail pickDetail:pickDetails){
            String commoditybar = pickDetail.getCommoditybar();
            String locatorcode = pickDetail.getLocatorcode();
            Integer qty = pickDetail.getQty();
            Stock stock = stockFeign.selectStockByCommodity(locatorcode, commoditybar);
            stock.setLqty(stock.getLqty()-qty);
            stockFeign.updateById(stock);
        }
        QueryWrapper<Review> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("outorderno",outorderno);
        Review review = reviewService.getOne(queryWrapper);
        review.setState("3");
        reviewService.updateById(review);
        QueryWrapper<Outorder> queryWrapper1=new QueryWrapper<>();
        queryWrapper1.eq("outorderno",outorderno);
        Outorder outorder = outorderService.getOne(queryWrapper1);
        outorder.setState("5");
        outorderService.updateById(outorder);
        return R.ok().message("录入成功").code(200);
    }

    @PostMapping("searchCommodityByOutorderno/{outorderno}")
    @ApiOperation(value = "根据出库订单编码获取商品信息")
    public R searchCommodityByOutorderno(@ApiParam(value = "出库订单编码") @PathVariable("outorderno") String outorderno){
        List<PickDetail> pickDetails = reviewDetailService.searchCommodityByOutorderno(outorderno);
        return R.ok().data("commoditys",pickDetails).code(200);
    }
}

